package p1000;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Queue;
import java.util.Stack;
import java.util.stream.Collectors;

public class Leetcode_1047 {
    public String removeDuplicates(String S) {

        Stack<Character> stack = new Stack<>();
        for (char c : S.toCharArray()) {
            if (stack.empty() || stack.peek() != c) {
                stack.push(c);
            } else {
                stack.pop();
            }
        }
        if (stack.empty()) {
            return "";
        } else {
            return stack.stream().map(Object::toString).collect(Collectors.joining(""));
        }
    }

    public static void main(String[] args) {

    }
}
